<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--菜单数据-->
      <el-col :span="24" :xs="24">

        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
          <el-form-item label="菜单名称" prop="menuName">
            <el-input v-model="queryParams.menuName" placeholder="请输入菜单名称" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="菜单编码" prop="menuCode">
            <el-input v-model="queryParams.menuCode" placeholder="请输入菜单编码" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"  size="mini" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery('queryForm')">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" v-hasPermi="['saveSystemMenu']"  @click="handleAdd">新增一级菜单</el-button>
          </el-col>

          <right-toolbar :showSearch.sync="showSearch" @queryTable="querySystemMenuTreeResList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table :stripe="true" v-loading="loading" :data="dataList" row-key="menuId" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
          <el-table-column  prop="menuName" label="菜单名称" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <SvgIcon :icon-class="scope.row.menuIcon" v-if="scope.row.menuIcon !== undefined"/>
              <span style="margin-left: 10px">{{ scope.row.menuName }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="menuCode" label="菜单编码" :show-overflow-tooltip="true"  width="200"></el-table-column>
          <el-table-column prop="component" label="路由路径" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="菜单类型" prop="menuTypeName" align="center" width="100">
            <template slot-scope="scope">
              <span :style="'color:'+scope.row.menuTypeColor">{{scope.row.menuTypeName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="菜单状态" prop="statusName" align="center" width="100">
            <template slot-scope="scope">
              <span :style="'color:'+scope.row.statusColor">{{scope.row.statusName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="内置菜单" prop="builtInName" align="center" width="150">
            <template slot-scope="scope">
              <span :style="'color:'+scope.row.builtInColor">{{scope.row.builtInName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="sort" align="center" width="100"></el-table-column>
          <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button v-if="scope.row.menuTypeCode==='NAVIGATION'" size="mini" type="text" icon="el-icon-plus" v-hasPermi="['saveSystemMenu']"  @click="handleAdd(scope.row)">新增</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" v-hasPermi="['updateSystemMenu']"  @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" v-hasPermi="['deleteSystemMenu']"  @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="addUpdateOpen" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="addUpdateOpenRules"  label-width="80px">
        <el-form-item label="上级菜单" prop="parentId">
          <treeselect v-model="form.parentId"
                      :options="menuOptions"
                      :defaultExpandLevel="10"
                      :normalizer="normalizerTree"
                      :show-count="true"
                      :hide-required-asterisk="true"
                      :disabled="form.menuId === undefined && form.parentId !== -1" autocomplete="off"
                      placeholder="请选择上级菜单" />
        </el-form-item>

        <el-form-item label="是否外链" prop="menuFrameCode" v-if="form.menuTypeCode !== 'BUTTON'">
          <el-select @change="handleChangeMenuTypeCode"  v-model="form.menuFrameCode" size="small" style="width: 100%"  placeholder="请选择">
            <el-option key="NO" label="否" value="NO"/>
            <el-option key="YES" label="是" value="YES"/>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单类型" prop="menuTypeCode" v-if="form.menuFrameCode !== 'YES' && form.parentId !== '-1'">
          <el-radio-group v-model="form.menuTypeCode">
            <el-radio label="NAVIGATION">菜单导航</el-radio>
            <el-radio label="BUTTON">功能按钮</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="菜单图标" prop="menuIcon" v-if="form.menuTypeCode !== 'BUTTON'">
          <el-popover placement="bottom-start" width="460" trigger="click" @show="$refs['iconSelect'].reset()">
            <IconSelect ref="iconSelect" @selected="menuIconSelected" />
            <el-input slot="reference" v-model="form.menuIcon" placeholder="点击选择图标" readonly maxlength="50" >
              <svg-icon v-if="form.menuIcon" slot="prefix" :icon-class="form.menuIcon" class="el-input__icon"  style="height: 32px;width: 16px;"  />
              <i v-else slot="prefix" class="el-icon-search el-input__icon" />
            </el-input>
          </el-popover>
        </el-form-item>

        <el-form-item label="菜单名称" prop="menuName">
          <el-input :hide-required-asterisk="true" autocomplete="off" v-model="form.menuName" placeholder="请输入菜单名称" maxlength="20" />
        </el-form-item>

        <el-form-item label="菜单编码" prop="menuCode">
          <el-input :hide-required-asterisk="true" :disabled="form.menuId !== undefined" autocomplete="off" v-model="form.menuCode" placeholder="请输入菜单编码" maxlength="50" />
        </el-form-item>


        <el-form-item label="菜单API" prop="apiUrl" v-if="form.menuTypeCode !== 'NAVIGATION'">
          <el-input :hide-required-asterisk="true" autocomplete="off" v-model="form.apiUrl" placeholder="请输入菜单API" maxlength="200" />
        </el-form-item>

        <el-form-item :label="form.menuFrameCode !== 'YES'?'路由地址':'外链地址'" prop="component"  v-if="form.menuTypeCode !== 'BUTTON'">
          <el-input autocomplete="off" v-model="form.component" :placeholder="form.menuFrameCode !== 'YES'?'请输入路由地址,例如:duojuhe/system/user/index':'请输入外链地址,例如:http://www.duojuhe.com'" maxlength="200" />
        </el-form-item>

        <el-form-item label="租户可用" prop="tenantMenu">
          <el-select v-model="form.tenantMenu" size="small" style="width: 100%"  placeholder="请选择">
            <el-option key="NO" label="否" value="NO"/>
            <el-option key="YES" label="是" value="YES"/>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单排序" prop="sort">
          <el-input-number :hide-required-asterisk="true" v-model="form.sort" :min="1" :max="9999" label="菜单排序" />
        </el-form-item>

        <el-form-item label="菜单状态" prop="statusCode">
          <el-radio-group v-model="form.statusCode">
            <el-radio label="NORMAL">启用</el-radio>
            <el-radio label="FORBID">禁用</el-radio>
          </el-radio-group>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button  size="medium" type="primary" @click="submitForm">确 定</el-button>
        <el-button  size="medium" @click="cancelAddUpdateOpen">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {querySystemMenuTreeResList, querySelectNavigationMenuTreeResList, saveSystemMenu, updateSystemMenu, deleteSystemMenuByMenuId} from '@/api/system/systemMenu'
  import Treeselect from "@riophae/vue-treeselect";
  import IconSelect from "@/components/IconSelect";
  import SvgIcon from '@/components/SvgIcon'
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  //默认查询参数
  let defaultQueryParams = {
    menuCode: undefined,
    menuName: undefined
  }
  export default {
    name: "systemMenuAdmin",
    components: {Treeselect, IconSelect, SvgIcon},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // table表格数据
        dataList: [],
        // 上级菜单选择
        menuOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示新增修改弹出层
        addUpdateOpen: false,
        // 增加修改表单参数
        form: {},
        // 查询参数
        queryParams: { ...defaultQueryParams },
        //列信息
        columns: [],
        // 新增修改表单校验
        addUpdateOpenRules: {
          menuName: [
            { required: true, message: "菜单名称不能为空",trigger: "blur" }
          ],
          menuCode: [
            { required: true, message: "菜单编码不能为空",trigger: "blur" }
          ],
          menuFrameCode: [
            { required: true, message: "是否外链必须选择"}
          ],
          statusCode: [
            { required: true, message: "菜单状态必须选择"}
          ],
          menuTypeCode: [
            { required: true, message: "菜单类型必须选择",trigger: "blur" }
          ],
          tenantMenu: [
            { required: true, message: "租户可用必须选择",trigger: "blur" }
          ],
          sort: [
            { required: true, message: "排序不能为空",trigger: "blur" }
          ]
        }
      }
    },
    created() {
      this.querySystemMenuTreeResList();
    },
    methods: {
      // 选择图标
      menuIconSelected(name) {
        this.form.menuIcon = name;
      },
      /** 查询菜单列表 */
      querySystemMenuTreeResList() {
        this.loading = true
        querySystemMenuTreeResList(this.queryParams).then(response => {
          this.dataList = response.data;
        }).finally(() =>{
          //关闭加载特效
          this.loading = false;
        });
      },

      /** 重置按钮操作 */
      resetQuery(fromId) {
        this.resetForm(fromId)
        this.handleQuery()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1
        this.querySystemMenuTreeResList()
      },

      /** 查询一级菜单列表 */
      querySelectNavigationMenuTreeResList() {
        querySelectNavigationMenuTreeResList().then(response => {
          this.menuOptions = [];
          const menu = { menuId: "-1", menuName: '一级菜单', children: [] };
          menu.children = response.data;
          this.menuOptions.push(menu);
        });
      },
      /** 转换菜单数据结构 */
      normalizerTree(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.menuId,
          label: node.menuName,
          children: node.children
        };
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        //加载一级菜单
        this.querySelectNavigationMenuTreeResList();
        //修改表单填充
        if (row != null && row.menuId) {
          this.form.parentId = row.menuId;
        }else {
          this.form.parentId = "-1";
          this.form.menuTypeCode = "NAVIGATION";
        }
        this.addUpdateOpen = true;
        this.title = "添加菜单";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        //加载一级菜单
        this.querySelectNavigationMenuTreeResList();
        //修改表单填充
        this.form = {...row};
        this.addUpdateOpen = true;
        this.title = "修改菜单";
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除所选菜单?', "警告", {
          cancelButtonText: "取消",
          confirmButtonText: "确定",
          type: "warning"
        }).then(function() {
          const menuId={menuId:row.menuId}
          return deleteSystemMenuByMenuId(menuId);
        }).then(response => {
          this.$modal.msgSuccess(response.message)
          this.querySystemMenuTreeResList();
        }).catch(function() {});
      },
      /** 提交保存或修改按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.menuId !== undefined) {
              updateSystemMenu(this.form).then(response => {
                this.$modal.msgSuccess(response.message)
                this.querySystemMenuTreeResList();
                this.addUpdateOpen = false
              });
            } else {
              saveSystemMenu(this.form).then(response => {
                this.$modal.msgSuccess(response.message)
                this.querySystemMenuTreeResList();
                this.addUpdateOpen = false
              });
            }
          }
        });
      },
      // 取消修改增加弹出层按钮
      cancelAddUpdateOpen() {
        this.addUpdateOpen = false
      },

      /** 改变菜单类型 */
      handleChangeMenuTypeCode() {
        this.form.menuTypeCode = this.form.menuFrameCode==='YES'?"NAVIGATION":this.form.menuTypeCode
      },
      // 表单重置
      reset() {
        this.form = {
          component: undefined,
          menuId: undefined,
          menuCode: undefined,
          menuName: undefined,
          apiUrl: undefined,
          menuIcon: undefined,
          parentId: "-1",
          menuTypeCode: "NAVIGATION",
          sort: 1,
          statusCode: "NORMAL",
          tenantMenu:"NO",
          menuFrameCode:"NO"
        };
        this.resetForm('form')
      }
    }
  }
</script>
